package com.eshore.demo.qingjia.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eshore.common.service.IYmlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eshore.khala.core.extension.plugins.pagination.Page;
import com.eshore.khala.core.kernel.conditions.query.QueryWrapper;
import com.eshore.khala.core.kernel.metadata.IPage;
import com.eshore.khala.core.kernel.toolkit.Wrappers;
import com.eshore.khala.core.starter.common.Result;
import com.eshore.khala.core.starter.util.CriteriaUtil;
import com.eshore.khala3.common.export.ExportUtil;
import com.eshore.khala3.common.reflect.ReflectUtil;
import com.eshore.khala.core.starter.util.PageUtil;
import com.eshore.khala.core.starter.web.controller.BaseController;
import com.eshore.demo.qingjia.entity.LzyQingjiaFrom;
import com.eshore.demo.qingjia.entity.LzyQingjiaFromCascade;
import com.eshore.demo.qingjia.service.ILzyQingjiaFromService;
import com.eshore.demo.qingjia.web.vo.LzyQingjiaFromVo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2022-05-07
 */
@RestController
@RequestMapping("/api/lzyQingjiaFrom")
public class LzyQingjiaFromController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(LzyQingjiaFromController.class);
    
    @Autowired
	private IYmlService ymlService;
    
    @Autowired
    private ILzyQingjiaFromService lzyQingjiaFromService;

    @HystrixCommand(fallbackMethod = "saveFallback")
    @ApiOperation(value = "新增")
    @PostMapping(value = "/save")
    public ResponseEntity<Result<LzyQingjiaFromCascade>> save(@RequestBody LzyQingjiaFromCascade lzyQingjiaFrom) {
        lzyQingjiaFromService.save(lzyQingjiaFrom);     
        return ResponseEntity.ok().body(Result.success(lzyQingjiaFrom));
    }

    public ResponseEntity<Result<LzyQingjiaFromCascade>> saveFallback(@RequestBody LzyQingjiaFromCascade lzyQingjiaFrom, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }

    @HystrixCommand(fallbackMethod = "listFallback")
    @ApiOperation(value = "列表查询")
    @GetMapping(value = "/list")
    public ResponseEntity<Result<List<LzyQingjiaFrom>>> list(LzyQingjiaFromVo lzyQingjiaFromVo)
            throws InterruptedException {
        QueryWrapper<LzyQingjiaFrom> queryWrapper = Wrappers.<LzyQingjiaFrom>query();

        CriteriaUtil.vo2Qw(lzyQingjiaFromVo, queryWrapper);

        List<LzyQingjiaFrom> result = lzyQingjiaFromService.list(queryWrapper);
        return ResponseEntity.ok().body(Result.success(result));
    }

    public ResponseEntity<Result<List<LzyQingjiaFrom>>> listFallback(LzyQingjiaFromVo lzyQingjiaFromVo, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }    

    @HystrixCommand(fallbackMethod = "pageFallback")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/page")
    public ResponseEntity<Result<IPage<LzyQingjiaFrom>>> page(LzyQingjiaFromVo lzyQingjiaFromVo, Page<?> page)
            throws InterruptedException {
        QueryWrapper<LzyQingjiaFrom> queryWrapper = Wrappers.<LzyQingjiaFrom>query();
        CriteriaUtil.vo2Qw(lzyQingjiaFromVo, queryWrapper);
        IPage<LzyQingjiaFrom> result = lzyQingjiaFromService.page(new PageUtil<LzyQingjiaFrom>().pageInstantof(page), queryWrapper);
        return ResponseEntity.ok().body(Result.success(result));
    }
    
    public ResponseEntity<Result<IPage<LzyQingjiaFrom>>> pageFallback(LzyQingjiaFromVo lzyQingjiaFromVo, Page<?> page, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }    

    @HystrixCommand(fallbackMethod = "getFallback")
    @ApiOperation(value = "根据id查询明细")
    @GetMapping(value = "/get/{id}")
    public ResponseEntity<Result<LzyQingjiaFrom>> get(@PathVariable("id") Long id) {
        LzyQingjiaFrom result = lzyQingjiaFromService.getById(id);
        return ResponseEntity.ok().body(Result.success(result));
    }

    public ResponseEntity<Result<LzyQingjiaFrom>> getFallback(@PathVariable("id") Long id, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }    

    @HystrixCommand(fallbackMethod = "updateFallback")
    @ApiOperation(value = "更新")
    @PutMapping(value = "/update")
    public ResponseEntity<Result<Boolean>> update(@RequestBody LzyQingjiaFrom lzyQingjiaFrom) {
        Boolean result = lzyQingjiaFromService.updateById(lzyQingjiaFrom);
        return ResponseEntity.ok().body(Result.success(result));
    }

    public ResponseEntity<Result<Boolean>> updateFallback(@RequestBody LzyQingjiaFrom lzyQingjiaFrom, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }        
    
    @HystrixCommand(fallbackMethod = "deleteFallback")
    @ApiOperation(value = "根据id删除，若有附表，会删除附表数据", notes = "")
    @DeleteMapping(value = "/delete/{id}")
    public ResponseEntity<Result<Boolean>> delete(@PathVariable("id") Long id) {
        Boolean result = lzyQingjiaFromService.removeById(id);
        return ResponseEntity.ok().body(Result.success(result));
    }
    
    public ResponseEntity<Result<Boolean>> deleteFallback(@PathVariable("id") Long id, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }

    @HystrixCommand(fallbackMethod = "deleteBatchFallback")
    @ApiOperation(value = "根据idList批量删除，若有附表，会删除附表数据。", notes = "参数如:")
    @DeleteMapping(value = "/deleteBatch")
    public ResponseEntity<Result<Boolean>> deleteBatch(List<Long> idList) {
        Boolean result = lzyQingjiaFromService.removeByIds(idList);
        return ResponseEntity.ok().body(Result.success(result));
    }

    public ResponseEntity<Result<Boolean>> deleteBatchFallback(List<Long> idList, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }
    
    @HystrixCommand(fallbackMethod = "totalFallback")
    @ApiOperation(value = "total查询")
    @GetMapping(value = "/total")
    public ResponseEntity<Result<Integer>> total(LzyQingjiaFromVo lzyQingjiaFromVo)
            throws InterruptedException {
        QueryWrapper<LzyQingjiaFrom> queryWrapper = Wrappers.<LzyQingjiaFrom>query();
        CriteriaUtil.vo2Qw(lzyQingjiaFromVo, queryWrapper);
        int total = lzyQingjiaFromService.count(queryWrapper);
        return ResponseEntity.ok().body(Result.success(total));
    }
    
    public ResponseEntity<Result<Integer>> totalFallback(LzyQingjiaFromVo lzyQingjiaFromVo, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }
    
	@HystrixCommand(fallbackMethod = "exportFallback")
	@ApiOperation(value = "导出")
	@GetMapping(value = "/export")
	public void export(LzyQingjiaFromVo lzyQingjiaFromVo, Page<?> page, HttpServletRequest request,
			HttpServletResponse response) throws InterruptedException {
		page.setSize(2 ^ 100).setCurrent(1);
		QueryWrapper<LzyQingjiaFrom> queryWrapper = Wrappers.<LzyQingjiaFrom>query();
		CriteriaUtil.vo2Qw(lzyQingjiaFromVo, queryWrapper);
		IPage<LzyQingjiaFrom> result = lzyQingjiaFromService.page(new PageUtil<LzyQingjiaFrom>().pageInstantof(page),
				queryWrapper);

		String exportPath = ymlService.getExportPath();
		String fileName = LzyQingjiaFrom.class.getSimpleName() + ".xlsx";
		try {
			ExportUtil.export(exportPath, fileName, ReflectUtil.getAccessibleFieldNames(LzyQingjiaFrom.class),
					ReflectUtil.getFieldValuesList(result.getRecords()), request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	  
	public void exportFallback(LzyQingjiaFromVo lzyQingjiaFromVo, Page<?> page, HttpServletRequest request,
			HttpServletResponse response,
			Throwable e) {
		logger.error("已启动降级,错误信息:", e);
	}	  
}
